package org.example.core.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.stream.Stream;

@Getter
@AllArgsConstructor
public enum ValidatorType {

    PHONE_REGEX("phone", "(?<!\\d)(1\\d{10})(?!\\d)"),
    MORE_PHONE_REGEX("phone", "^1[3-9]\\d{9}$"),
    EMAIL_REGEX("email", "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"),
    URL_REGEX("url","^(https?|ftp)://[\\w\\-]+(\\.[\\w\\-]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]+)*$"),

    ;

    /**
     * 编码
     */
    private final String code;

    /**
     * 编码说明
     */
    private final String desc;

    /**
     * 通过code查询状态
     */
    public static String findByCode(String code) {
        for (ValidatorType value : ValidatorType.values()) {
            if (code.equals(value.getCode())) {
                return value.getDesc();
            }
        }
        throw new RuntimeException(code + "类型，查不到对应枚举值");
    }

    public static ValidatorType convert(String desc) {
        return Stream.of(values())
                .filter(bean -> bean.desc.equals(desc))
                .findAny()
                .orElse(null);
    }

    public static ValidatorType of(String code) {
        return Stream.of(values())
                .filter(bean -> bean.code.equals(code))
                .findAny()
                .orElse(null);
    }
}

